import socketserver
import threading
from .protocol import *

class UNOServer(socketserver.ThreadingTCPServer):
    def __init__(self, host, port):
        super().__init__((host, port), RequestHandler)
        self.rooms = {}
        self.lock = threading.Lock()

class RequestHandler(socketserver.BaseRequestHandler):
    def handle(self):
        while True:
            try:
                msg_type, data = unpack_message(self.request)
                if msg_type == MessageType.JOIN:
                    self._handle_join(data)
                elif msg_type == MessageType.PLAY_CARD:
                    self._handle_play_card(data)
                # 其他消息处理...
            except ConnectionError:
                break

    def _broadcast(self, room_id, msg_type, data):
        packed = pack_message(msg_type, data)
        for player in self.server.rooms[room_id]['players']:
            player['conn'].sendall(packed)
